package everydaydo;
/*
 * 计算五子棋落子后是否胜利的方法
 * */
public class FiveInARow {
	/*
	 * 参数依次为：代表当前棋局的数组，落子的行数和列数，当前落子的颜色
	 * */
	public static boolean isWin(int[][] chessBoard,int row,int column,int colour ){
		if(isWinRow(chessBoard, row, column, colour)){
			return true;
		}
		if(isWinColumn(chessBoard, row, column, colour)){
			return true;
		}
		if(isWinForerake(chessBoard, row, column, colour)){
			return true;
		}
		if(isWinHypsokinesis(chessBoard, row, column, colour)){
			return true;
		}
		return false;
	}
	//计算横着一排是否赢了
	private static boolean isWinRow(int[][] chessBoard,int row,int column,int colour ){
		int lineNum = 1;
		boolean reduceContinue = true;
		boolean addContinue = true;
		for(int i=1;i<=5;i++){
			if(!addContinue&&!reduceContinue){
				return false;
			}
			if(reduceContinue&&(row-i)>=0&&chessBoard[row-i][column]==colour){
				lineNum++;
				if(lineNum>=5)
					return true;
			}else{
				reduceContinue=false;
			}
			if(addContinue&&(row+i)<=14&&chessBoard[row+i][column]==colour){
				lineNum++;
				if(lineNum>=5)
					return true;
			}else{
				addContinue=false;
			}
		}
		return false;
	}
	//竖着一排的计算
	private static boolean isWinColumn(int[][] chessBoard,int row,int column,int colour ){
		int lineNum = 1;
		boolean reduceContinue = true;
		boolean addContinue = true;
		for(int i=1;i<=5;i++){
			if(!addContinue&&!reduceContinue){
				return false;
			}
			if(reduceContinue&&(column-i)>=0&&chessBoard[row][column-i]==colour){
				lineNum++;
				if(lineNum>=5)
					return true;
			}else{
				reduceContinue=false;
			}
			if(addContinue&&(column+i)<=14&&chessBoard[row][column+i]==colour){
				lineNum++;
				if(lineNum>=5)
					return true;
			}else{
				addContinue=false;
			}
		}
		return false;
	}
	//向前斜的一排的计算情况
	private static boolean isWinForerake(int[][] chessBoard,int row,int column,int colour ){
		int lineNum = 1;
		boolean reduceContinue = true;
		boolean addContinue = true;
		
		for(int i=1;i<=5;i++){
			if(!addContinue&&!reduceContinue){
				return false;
			}
			if(reduceContinue&&(column+i)<=14&&(row-i)>=0&&chessBoard[row-i][column+i]==colour){
				lineNum++;
				if(lineNum>=5)
					return true;
			}else{
				reduceContinue=false;
			}
			if(addContinue&&(column-i)<=14&&(row+i)<=14&&chessBoard[row+i][column-i]==colour){
				lineNum++;
				if(lineNum>=5)
					return true;
			}else{
				addContinue=false;
			}
		}
		return false;
	}
	//计算后倾的一排的情况
	private static boolean isWinHypsokinesis(int[][] chessBoard,int row,int column,int colour ){
		int lineNum = 1;
		boolean reduceContinue = true;
		boolean addContinue = true;
		
		for(int i=1;i<=5;i++){
			if(!addContinue&&!reduceContinue){
				return false;
			}
			if(reduceContinue&&(column-i)>=0&&(row-i)>=0&&chessBoard[row-i][column-i]==colour){
				lineNum++;
				if(lineNum>=5)
					return true;
			}else{
				reduceContinue=false;
			}
			if(addContinue&&(column+i)<=14&&(row+i)<=14&&chessBoard[row+i][column+i]==colour){
				lineNum++;
				if(lineNum>=5)
					return true;
			}else{
				addContinue=false;
			}
		}
		return false;
	}
}
